﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace LGPSDataGen.Generators
{
    //See comments from Flyers generator
    class SevenSixersGenerator : Generator, IGenerator
    {
        private string pathToFile;
        private string content = "http://espn.go.com/nba/teams/printSchedule?team=phi&season=2011";
        private DateTime mDate;

        public SevenSixersGenerator(DateTime theDate)
        {
            mDate = theDate;
            string timeStamp = mDate.Year + "_" + mDate.Month + "_" + mDate.Day;
            pathToFile = @"76ers" + timeStamp + ".html";
        }

        public override void DownloadData()
        {
            WebClient wc = new WebClient();
            wc.DownloadFile(content, pathToFile);
        }

        public override void ProcessData()
        {
            StreamReader stream = new StreamReader(pathToFile + ".txt");

            string s = stream.ReadLine();
            int month = 0;
            int day = 0;
            int hour = 0;
            bool found = false;
            month = DateUtility.ConvertMonthToInt(s);
            string temp_string1 = string.Join(null, System.Text.RegularExpressions.Regex.Split(s, "[^\\d]"));
            temp_string1 = temp_string1.Remove(0, 6);

            day = Int32.Parse(temp_string1);

            s = stream.ReadLine();
            hour = Int32.Parse(s.Split(':')[0]) + 12;

            DateTime d1 = new DateTime(2011, month, day, hour, 0, 0);
            List<string> dataArray = new List<string>();
            string fullDate = "";
            while (!stream.EndOfStream)
            {
                month = DateUtility.ConvertMonthToInt(s);
                string temp_string = string.Join(null, System.Text.RegularExpressions.Regex.Split(s.Split(':')[1], "[^\\d]"));
                temp_string = temp_string.Remove(0, 2);
                day = Int32.Parse(temp_string);

                s = stream.ReadLine();
                hour = Int32.Parse(s.Split(':')[0]) + 12;
                if (hour == 24)
                    hour = 12;

                fullDate = "2011" + "_" + mDate.Month + "_" + mDate.Day;

                DateTime d = new DateTime(2011, month, day, hour, 0, 0);

                if (d.DayOfYear == mDate.DayOfYear)
                {
                    found = true;
                    for (int i = 0; i < 24; i++)
                    {
                        string data;
                        if ((i == hour) || (i > hour && i < hour + 3))
                            data = i + ",1";
                        else
                            data = i + ",0";
                        dataArray.Add(data);
                    }
                }
            }
            if(!found)
            {
                for (int i = 0; i < 24; i++)
                {
                    string data = i + ",0";
                    dataArray.Add(data);
                }
            }

            File.WriteAllLines(@"76ers-" + fullDate + ".csv", dataArray.ToArray());
            
        }

        public override void ExtractData()
        {
            StreamReader stream = new StreamReader(pathToFile);
            StreamWriter writer = new StreamWriter(pathToFile + ".txt");

            while (!stream.EndOfStream)
            {
                string s = stream.ReadLine();
                writer.WriteLine(Regex.Replace(s, "<.*?>", " "));
            }

            writer.Close();
            stream.Close();
        }
    }
}
